import React from 'react';
import { Button, Form, Input, Message } from 'antd';
import { Router, Route, hashHistory, browserHistory, IndexRoute, IndexRedirect , Redirect } from 'react-router';
const createForm = Form.create;
const FormItem = Form.Item;

let SettingIndex = React.createClass({
        getInitialState() {
            return {
                userData: {},
            }
        },

       componentDidMount() {
        this.fetch();
    },

    handleSubmit(){
        let me = this;
        let pathname = '/funder/resource/list';
        let params = this.props.form.getFieldsValue();
        console.log(params);
        this.props.form.validateFields((errors, values) => {
            if (!!errors) {
                console.log('Errors in form!!!');
                return;
            }
            
            Utils.sendAjax({
                url: '/funder/updatePwd',
                data: JSON.stringify(params),
                callback: function(result) {
                    if (result.code == '200') {
                         Message.success(result.message) 
                         if(params.oldPwd !== undefined && params.password !== undefined && params.confirmPwd !== undefined) {
                                hashHistory.push("/") 
                         }else {
                               hashHistory.push({pathname: pathname,state: 'resouce'})
                         }
                           
                    } else {
                        Message.error(result.message, 10)
                    }
                }
            })
        });
    },
    fetch() {
        let username = localStorage.funderMobilePhone;
        let me = this;
        Utils.sendAjax({
            url: 'funder/accout/setting',
            data: JSON.stringify({username: username}),
            callback: function(result) {
                if (result.code == '200'){
                    me.setState({
                        userData: result.data
                    })
                }else{

                }
            }
        })
    },
    render() {
        const { getFieldProps } = this.props.form;
        let params = this.props.form.getFieldsValue();
        console.log(params)
        let userData = this.state.userData;
        const oldPwdProps = getFieldProps('oldPwd');
        const passwordProps = getFieldProps('password');
        const confirmPwdProps = getFieldProps('confirmPwd');
          const realNameProps = getFieldProps('realName', {initialValue: userData.realName,
            rules: [{
                required: true,
                
                message: '联系人不能为空',
            }],
        });
           const emailProps = getFieldProps('email', {initialValue: userData.email,
            rules: [{
                required: true,
                
                message: '电子邮箱不能为空',
            }],
        });
        return (
            <div>
                <div className="resetPwd">
                    <h1>密码修改</h1>
                    <Form horizontal form={this.props.form}>
                    {
                      <ul>  
                            <li><label className="fl ant-form-item">公司名称</label>
                                <FormItem  className="fr"  label="">
                                   <Input  value={userData.company} style={{border: 'none'}} disabled/>
                                </FormItem>
                            </li>
                             <li><label className="fl ant-form-item">联系人<em style={{color: 'red'}}>*</em></label>
                                <FormItem  className="fr"  label="">
                                    <Input  {...realNameProps} />
                                </FormItem>
                            </li>
                              <li><label className="fl ant-form-item">电子邮箱<em style={{color: 'red'}}>*</em></label>
                                <FormItem  className="fr"  label="">
                                    <Input  {...emailProps} />
                                </FormItem>
                            </li>
                            <li><label className="fl ant-form-item">旧密码</label>
                                <FormItem  className="fr"  label="">
                                    <Input type="password" {...oldPwdProps} placeholder="请输入旧密码" />
                                </FormItem>
                            </li>
                            <li><label className="fl ant-form-item">新密码</label>
                                <FormItem className="fr" label="">
                                    <Input type="password"  {...passwordProps} placeholder="8-16位数字、字母、特殊字符组成" />
                                </FormItem>
                             </li>
                             <li><label className="fl ant-form-item">确认新密码</label>
                                <FormItem className="fr" label="">
                                    <Input type="password"  {...confirmPwdProps} placeholder="请确认新密码" />
                                </FormItem>
                             </li>
                            <li>
                                <span className="fl ant-form-item"></span>
                                <FormItem className="fr" label="">
                                    <Button className="changeBtn" type="primary" style={{width:280,height:40}} onClick={this.handleSubmit}>确认修改</Button>
                                </FormItem>                                
                            </li>
                        </ul>  
                    }
                        
                    </Form>
                </div>
  
            </div>
        )
    }
})
SettingIndex = createForm()(SettingIndex);
export default SettingIndex;
